
import { createRouter, createWebHistory } from 'vue-router'
import {useUserStore} from "@/stores/user"


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  
  routes: [
    
    {
      path: '/login',
      name: 'login',
      component: ()=>import('@/views/LoginPage.vue')
    },
    {
      path: '/',
      redirect: '/home',
      component: ()=>import('@/views/client/LayoutContainer.vue'),
      children: [
        {path: '/home',name:'home',component: ()=>import('@/views/client/HomePage.vue')},
        {path: '/releaseDynamic',name:'releaseDynamic',component: ()=>import('@/views/client/dynamic/ReleaseDynamic.vue')},
        {path: '/dynamicDetail/:id',name: 'dynamicDetail', component: ()=>import('@/views/client/dynamic/DynamicDetail.vue')},
        {
          path: '/personalCenter/:userId?',
        name: 'personalCenter', 
        component: ()=>import('@/views/client/personal/PersonalCenter.vue'),
        // redirect: '/personalDynamics',
      },
        {path: '/MessagePage',name: 'messagePage', component: ()=>import('@/views/client/MessagePage.vue')},
        {
          path: '/myInformationLayout',
          component: ()=>import('@/views/client/myInformation/MyInformationLayout.vue'),
          redirect: '/baseInformation',
          children: [
            {path: '/baseInformation',name:'baseInformation',component: ()=>import('@/views/client/myInformation/BaseInformation.vue')},
            {path: '/myPassword',name:'myPassword',component: ()=>import('@/views/client/myInformation/MyPassword.vue')},
            {path: '/myEmail',name:'myEmail',component: ()=>import('@/views/client/myInformation/MyEmail.vue')},
            {path: '/cancelAccount',name:'cancelAccount',component: ()=>import('@/views/client/myInformation/CancelAccount.vue')}
            
          ]
        }
      ]
    },
    {
      path: '/register',
      name: 'register',
      component: ()=>import('@/views/RegisterPage.vue')
    },
    {
      path: '/admin',
      name: 'admin',
      redirect: '/homePage',
      component: ()=>import('@/views/admin/LayoutContainer.vue'),
      children:[
        {path: '/userManagement',name:'userManagement',component: ()=>import('@/views/admin/UserManagement.vue')},
        {path: '/dynamicManagement',name:'dynamicManagement',component: ()=>import('@/views/admin/DynamicManagement.vue')},
        {path: '/reportHandling',name:'reportHandling',component: ()=>import('@/views/admin/ReportHandling.vue')},
        {path: '/homePage',name:'homePage',component: ()=>import('@/views/admin/HomePage.vue')}
      ]
    }
    
  ]
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  // 如果没有token, 且访问的是非登录页，拦截到登录，其他情况正常放行
  if (!userStore.token && (to.path !== '/login' && to.path !== '/register' )) return '/login'
})

export default router
